import {
  BadRequestException,
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
  UseGuards,
} from '@nestjs/common';
import { ApiOperation, ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger';
import { DishService } from '@/modules/dish/dish.service';
import { DishEntity } from '@/entity/dish.entity';
import { CreateDishDto } from '@/modules/dish/dto/create-dish.dto';
import { UpdateDishDto } from '@/modules/dish/dto/update-dish.dto';
import { JwtAuthGuard } from '@/auth/guard/jwt-auth.guard';
import { Roles, RolesGuard } from '@/auth/guard/roles.guard';

@ApiTags('菜品内容')
@Controller('dish')
export class DishController {
  constructor(private readonly dishService: DishService) {}

  @ApiOperation({ summary: '分页查询所有菜品内容' })
  @ApiQuery({
    name: 'page',
    required: false,
    example: 1,
    description: '页码，默认为1',
  })
  @ApiQuery({
    name: 'limit',
    required: false,
    example: 10,
    description: '每页数据条数，默认为10',
  })
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('admin')
  @Get()
  async findAll(@Query('page') page = 1, @Query('limit') limit = 10) {
    if (isNaN(Number(page)) || isNaN(Number(limit))) {
      throw new BadRequestException('page or limit must be number');
    }
    if (page < 1) page = 1;
    if (limit < 1) limit = 10;
    return this.dishService.findAll(page, limit);
  }

  @ApiOperation({ summary: '查询指定菜品内容' })
  @ApiResponse({ status: 200, description: '成功', type: DishEntity })
  @ApiResponse({ status: 404, description: '未找到指定菜品内容' })
  @Get(':id')
  async findOne(@Param('id') id: number): Promise<DishEntity> {
    return this.dishService.findOne(id);
  }

  @ApiOperation({ summary: '创建菜品内容' })
  @ApiResponse({ status: 201, description: '成功', type: DishEntity })
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('admin')
  @Post()
  async create(@Body() createDishDto: CreateDishDto): Promise<DishEntity> {
    return this.dishService.create(createDishDto);
  }

  @ApiOperation({ summary: '更新菜品内容' })
  @ApiResponse({ status: 200, description: '成功', type: DishEntity })
  @ApiResponse({ status: 404, description: '未找到指定菜品内容' })
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('admin')
  @Put(':id')
  async update(
    @Param('id') id: number,
    @Body() updateDishDto: UpdateDishDto,
  ): Promise<DishEntity> {
    return this.dishService.update(id, updateDishDto);
  }

  @ApiOperation({ summary: '删除菜品内容' })
  @ApiResponse({ status: 200, description: '成功', type: DishEntity })
  @ApiResponse({ status: 404, description: '未找到指定菜品内容' })
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('admin')
  @Delete(':id')
  async delete(@Param('id') id: number) {
    return this.dishService.delete(id);
  }

  @ApiOperation({ summary: '查询指定菜品类别 ID 下的菜品内容列表' })
  @ApiResponse({ status: 200, description: '成功', type: [DishEntity] })
  @Get('category/:categoryId')
  async findByCategoryId(
    @Param('categoryId') categoryId: number,
  ): Promise<DishEntity[]> {
    return this.dishService.findByCategoryId(categoryId);
  }

  @ApiOperation({ summary: '查询指定餐厅下的菜品' })
  @Get('restaurant/:restaurantId')
  async findByRestaurantId(@Param('restaurantId') restaurantId: number) {
    return await this.dishService.findByRestaurantId(restaurantId);
  }

  @ApiOperation({ summary: '以菜品类别为组别存放拥有的菜品内容' })
  @Get('group-by-category/restaurant/:restaurantId')
  async findGroupByCategoryId(@Param('restaurantId') restaurantId: number) {
    return await this.dishService.findByRestaurantIdGroupByCategory(
      restaurantId,
    );
  }
}
